package net.mcreator.infinite_storage.procedure;

import net.minecraft.world.World;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.item.ItemStack;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.Container;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.Entity;

import net.mcreator.infinite_storage.gui.GuiInfiniteBagGui;
import net.mcreator.infinite_storage.InfiniteStorageMod;
import net.mcreator.infinite_storage.ElementsInfiniteStorageMod;

import java.util.function.Supplier;
import java.util.Map;
import java.util.HashMap;

@ElementsInfiniteStorageMod.ModElement.Tag
public class ProcedureInfiniteBagSearch extends ElementsInfiniteStorageMod.ModElement {
	public ProcedureInfiniteBagSearch(ElementsInfiniteStorageMod instance) {
		super(instance, 59);
	}
	static Map<String, Object> $_dependencies = new HashMap<>();
	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			return;
		}
		if (dependencies.get("x") == null) {
			return;
		}
		if (dependencies.get("y") == null) {
			return;
		}
		if (dependencies.get("z") == null) {
			return;
		}
		if (dependencies.get("guistate") == null) {
			return;
		}
		if (dependencies.get("world") == null) {
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		int x = (int) dependencies.get("x");
		int y = (int) dependencies.get("y");
		int z = (int) dependencies.get("z");
		String guistate = (String) dependencies.get("guistate");
		World world = (World) dependencies.get("world");
		String StringTemp = "";
		String NameAA = "";
		String NameEE = "";
		NBTTagCompound InfiniteBag = entity.getEntityData().getCompoundTag("InfiniteBag");
		{
			$_dependencies.put("entity", entity);
			ProcedureInfiniteBagGuiClose.executeProcedure($_dependencies);
		}
		int NumTemp = new Object() {
			int convert(String s) {
				try {
					return Integer.parseInt(s.trim());
				} catch (Exception e) {
				}
				return 0;
			}
		}.convert((new Object() {
			String[] value = guistate.split(",");
			int indexNum = 0;
			int index0 = (int) value.length;
			{
				for (int i = 0; i < index0; i++) {
					if ((value[i].contains("BagSearch:"))) {
						indexNum = i;
						break;
					}
				}
			}
			String indexvalue = value[(int) indexNum];
			String getString() {
				if (indexNum >= 0 && indexNum < index0) {
					String s = indexvalue;
					s = (s.replace("BagSearch:", ""));
					return s;
				} else {
					return "";
				}
			}
		}.getString()));
		StringTemp = (String) (new Object() {
			String[] value = guistate.split(",");
			int indexNum = 0;
			int index0 = (int) value.length;
			{
				for (int i = 0; i < index0; i++) {
					if ((value[i].contains("BagSearch:"))) {
						indexNum = i;
						break;
					}
				}
			}
			String indexvalue = value[(int) indexNum];
			String getString() {
				if (indexNum >= 0 && indexNum < index0) {
					String s = indexvalue;
					s = (s.replace("BagSearch:", ""));
					return s;
				} else {
					return "";
				}
			}
		}.getString());
		if ((NumTemp != 0)) {
			if ((NumTemp > InfiniteBag.getDouble("InfiniteBagPageAdd"))) {
				InfiniteBag.setString("InfiniteBagTip", "\u00A7e>\u7B49\u7EA7\u8DF3\u8F6C\u81F3\u6700\u540E\u4E00\u9875");
				InfiniteBag.setDouble("InfiniteBagPage", InfiniteBag.getDouble("InfiniteBagPageAdd"));
			} else if ((NumTemp < 0)) {
				InfiniteBag.setString("InfiniteBagTip", "\u00A7e<0\u8DF3\u8F6C\u81F3\u7B2C0\u9875");
				InfiniteBag.setDouble("InfiniteBagPage", 0);
			} else {
				InfiniteBag.setString("InfiniteBagTip", (("\u00A7a\u5DF2\u8DF3\u8F6C\u81F3\u7B2C") + "" + (NumTemp) + "" + ("\u9875")));
				InfiniteBag.setDouble("InfiniteBagPage", NumTemp);
			}
		} else if ((((StringTemp)).equals("0"))) {
			InfiniteBag.setString("InfiniteBagTip", (("\u00A7a\u5DF2\u8DF3\u8F6C\u81F3\u7B2C") + "" + (NumTemp) + "" + ("\u9875")));
			InfiniteBag.setDouble("InfiniteBagPage", 0);
		} else if ((!(((StringTemp)).equals("0")))) {
			double NumTemp2 = (InfiniteBag.getDouble("InfiniteBagPageAdd") + 1);
			for (int index0 = 0; index0 < (int) (NumTemp2); index0++) {
				NameAA = (String) (("ItemSave") + "" + ((new java.text.DecimalFormat("0").format(index0))));
				NameEE = (String) (("ItemName") + "" + ((new java.text.DecimalFormat("0").format(index0))));
				if (((InfiniteBag.getString((NameAA)).contains((StringTemp))) || (InfiniteBag.getString((NameEE)).contains((StringTemp))))) {
					InfiniteBag.setDouble("InfiniteBagPage", index0);
					InfiniteBag.setString("InfiniteBagTip", "\u00A7a\u5DF2\u8DF3\u8F6C\u81F3\u76EE\u6807\u9875");
					break;
				} else if ((index0 >= InfiniteBag.getDouble("InfiniteBagPageAdd"))) {
					InfiniteBag.setString("InfiniteBagTip", "\u00A7c\u672A\u627E\u5230\u76EE\u6807");
				}
			}
		}
		InfiniteBag.setDouble("InfiniteBagTipClear", 0);
		for (int index1 = 0; index1 < (int) (54); index1++) {
			if (entity instanceof EntityPlayerMP) {
				Container _current = ((EntityPlayerMP) entity).openContainer;
				if (_current instanceof Supplier) {
					Object invobj = ((Supplier) _current).get();
					if (invobj instanceof Map) {
						ItemStack _setstack = (ItemStack.EMPTY);
						_setstack.setCount((int) 1);
						((Slot) ((Map) invobj).get((int) (index1))).putStack(_setstack);
						_current.detectAndSendChanges();
					}
				}
			}
		}
		entity.getEntityData().setTag("InfiniteBag", InfiniteBag);
		{
			$_dependencies.put("entity", entity);
			ProcedureInfiniteBagGuiOpen.executeProcedure($_dependencies);
		}
		if (entity instanceof EntityPlayer)
			((EntityPlayer) entity).openGui(InfiniteStorageMod.instance, GuiInfiniteBagGui.GUIID, world, x, y, z);
	}
}
